//
//  OpenGPSClass.m
//  HiCar
//
//  Created by 丁一 on 2019/2/13.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "OpenGPSClass.h"

@interface OpenGPSClass ()

@property (nonatomic, strong) UIViewController *herevc;

@end

@implementation OpenGPSClass

- (instancetype)init:(UIViewController *)vc{
    if (self=[super init]) {
        _herevc = vc;
        
        [self GPS_open];
    }
    return self;
}

-(void)GPS_open{
    //判断定位是否开启
    if ([CLLocationManager locationServicesEnabled]){
        //ok
    }else{
        //@"你已关闭定位";
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"查看附近偶遇需要您的位置哦！" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *resetActionqu = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertController addAction:resetActionqu];
        [_herevc.tabBarController presentViewController:alertController animated:YES completion:nil];
    }
    
    self.manager.delegate = self;
    // 判断操作系统的版本
    if( [[UIDevice currentDevice].systemVersion doubleValue] >= 8.0){
        [self.manager requestWhenInUseAuthorization];
        
    }else{
        [self.manager startUpdatingLocation];
    }
}

////// ------    定位   -----  ／／／／／／／
- (CLLocationManager *)manager{
    if (_manager == nil) {
        _manager = [[CLLocationManager alloc] init];
    }
    return _manager;
}

- (CLGeocoder *) geoCoder{
    if (_geoCoder == nil) {
        _geoCoder = [[CLGeocoder alloc]init];
    }
    return _geoCoder;
}

#pragma mark-CLLocationManagerDelegate用户是否同意
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            //和对应
            //设置精度
            self.manager.distanceFilter = 20;
            [self.manager startUpdatingLocation];
            break;
        case kCLAuthorizationStatusDenied:
            
            break;
        default:
            break;
    }
}

#pragma mark 用户的位置法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    for (CLLocation *location in locations) {
        
        [self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (error == nil) {
                CLLocation *loc = [locations lastObject];
                CLPlacemark *mark    = placemarks.lastObject;
                
                [DEFAULTS setValue:[NSString stringWithFormat:@"%f",loc.coordinate.latitude] forKey:City_WeiDu_Key];
                [DEFAULTS setValue:[NSString stringWithFormat:@"%f",loc.coordinate.longitude] forKey:City_JinDu_Key];
                //                NSLog(@"纬度=%f，经度=%f",loc.coordinate.latitude,loc.coordinate.longitude);
                NSLog(@"%@",mark.locality);
                
                self.Blcok(loc,mark,nil);
                
            }else{
                self.Blcok(nil,nil,error);
            }
        }];
    }
    //手动停止定位
    [self.manager stopUpdatingLocation];
}

@end
